﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.IO;
using System.Xml.Serialization;
using System.Runtime.Serialization.Formatters.Binary;

namespace XQ.Framework.io
{
    /// <summary>
    /// 对象的序列化和反序列化,objType是对象类型
    /// </summary>
    public class SerializeHelper
    {
        /// <summary>
        /// 序列化
        /// </summary>
        /// <param name="path"></param>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static bool SerializeToBinary<T>(string path, T obj)
        {
            FileStream fs = null;
            try
            {
                BinaryFormatter bf = new BinaryFormatter();
                fs = new FileStream(path, FileMode.Create, FileAccess.Write);
                bf.Serialize(fs, obj);
                return true;
            }
            catch
            {
                return false;
            }
            finally
            {
                IOHelper.closeStream(fs);
            }
        }
        /// <summary>
        /// 反序列化
        /// </summary>
        /// <param name="path"></param>
        /// <returns></returns>
        public static T DeSerializeFromBinary<T>(string path)
        {
            FileStream fs = null;
            try
            {
                fs = new FileStream(path, FileMode.Open, FileAccess.Read);
                BinaryFormatter bf = new BinaryFormatter();
                object obj = bf.Deserialize(fs);
                if (obj is T)
                {
                    return (T)obj;
                }
                return default(T);
            }
            catch
            {
                return default(T);
            }
            finally
            {
                IOHelper.closeStream(fs);
            }
        }

        /// <summary>
        /// XML序列化
        /// </summary>
        /// <param name="obj">序列对象</param>
        /// <param name="filePath">XML文件路径</param>
        /// <returns>是否成功</returns>
        public static bool SerializeToXml<T>(T obj, string filePath)
        {
            FileStream fs = null;
            try
            {
                fs = new FileStream(filePath, FileMode.Create, FileAccess.Write, FileShare.ReadWrite);
                XmlSerializer serializer = new XmlSerializer(obj.GetType());
                serializer.Serialize(fs, obj);
                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
            finally
            {
                IOHelper.closeStream(fs);
            }

        }

        public static T DeserializeFromXML<T>(string filePath)
        {
            FileStream fs = null;
            try
            {
                fs = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
                XmlSerializer serializer = new XmlSerializer(typeof(T));
                object obj = serializer.Deserialize(fs);
                if (obj is T)
                {
                    return (T)obj;
                }
                return default(T);
            }
            catch (Exception ex)
            {
                return default(T);
            }
            finally
            {
                IOHelper.closeStream(fs);
            }
        }
    }
}